package com.color.admin.common.core.utils.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

/**
 * @author HaiQiu
 * @date 2021/12/3
 */
public class JsonUtils {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static byte[] writeValueAsBytes(Object o){
        try {
            return OBJECT_MAPPER.writeValueAsBytes(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


    @SuppressWarnings("unchecked")
    public static Map<String,Object> objFormatMap(Object o){
        try {
            return OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(o), Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
